package com.cr.crandroid.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cr.crandroid.R;
import com.cr.crandroid.bean.Car;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.utils.OkHttpManager;
import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnRangeDatePickListener;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class PublishAdapter extends BaseQuickAdapter<Car, BaseViewHolder> {

    private CalendarPicker calendarPicker;
    private MaterialAlertDialogBuilder materialAlertDialog;
    private Car nowCar = new Car();

    public PublishAdapter(@Nullable List<Car> data, CalendarPicker picker) {
        super(R.layout.item_publish_item, data);
        calendarPicker = picker;
    }

    public PublishAdapter(@Nullable List<Car> data, MaterialAlertDialogBuilder alertDialog) {
        super(R.layout.item_publish_item, data);
        materialAlertDialog = alertDialog;
    }

    public PublishAdapter(@Nullable List<Car> data) {
        super(R.layout.item_publish_item, data);
    }


    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, Car car) {
        baseViewHolder.setText(R.id.textView, car.getBrand());
        baseViewHolder.setText(R.id.textView2, car.getColor());
        baseViewHolder.setText(R.id.textView3, car.getSeatingCapacity());
        Button button = baseViewHolder.findView(R.id.button);
        ImageView imageView = baseViewHolder.findView(R.id.imageView2);
        switch (car.getStatus()) {
            case CommonConstant.APPROVE_STATUS_0:
                button.setBackgroundColor(Color.parseColor("#FF6200EE"));
                button.setText("发布");
                imageView.setImageResource(R.drawable.under_publish);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showCalendarPicker(car);
                    }
                });
                baseViewHolder.setText(R.id.textView6, CommonConstant.NOT_APPROVE);
                break;
            case CommonConstant.APPROVE_STATUS_1:
                button.setBackgroundColor(Color.RED);
                button.setText("取消申请");
                imageView.setImageResource(R.drawable.under_approve);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showMaterialAlertDialog(car);
                    }
                });
                break;
            case CommonConstant.APPROVE_STATUS_2:
                button.setVisibility(View.GONE);
                imageView.setImageResource(R.drawable.pass);
                break;
            case CommonConstant.APPROVE_STATUS_3:
                button.setBackgroundColor(Color.RED);
                button.setText("再次申请");
                imageView.setImageResource(R.drawable.not_pass);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showMaterialAlertDialog(car);
                    }
                });
                break;
        }


        if (car.getRentalStart() != null && car.getRentalEnd() != null) {
            baseViewHolder.setText(R.id.textView6, car.getRentalStart() + "至" + car.getRentalEnd());
        }
        if (car.getImage() != null)
            Glide.with(getContext()).load(car.getImage().split(",")[0])
                    .into((ImageView) baseViewHolder.getView(R.id.imageView));
    }

    public Car getNowCar() {
        return nowCar;
    }

    private void showCalendarPicker(Car car) {
        nowCar = car;
        if (nowCar.getRentalStart() != null && nowCar.getRentalEnd() != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                calendarPicker.setSelectedDate(sdf.parse(nowCar.getRentalStart()), sdf.parse(nowCar.getRentalEnd()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        calendarPicker.show();
    }

    private void showMaterialAlertDialog(Car car) {
        nowCar = car;
        materialAlertDialog.show();
    }
}
